$(function () {
    var posDatagrid,posDialog,posForm,posDialog2,posForm2;
    posDatagrid = $("#pos_datagrid");
    posDialog = $("#pos_dialog");
    posForm = $("#pos_form");
    posDialog2 = $("#pos2_dialog");
    posForm2 = $("#pos2_form");

    posDatagrid.datagrid({
        fit:true,
        singleSelect:true,
        pagination:true,
        url:'/pos/list',
        columns:[
            [
                {field:'ck',checkbox:true},
                {field:'macsite',align:'center',width:200,title:'MAV地址'},
                {field:'organization',align:'center',width:200,title:'业务机构',formatter:organizationFormatter},
                {field:'bankName',align:'center',width:200,title:'银行机构代码',formatter:banknameFormatter},
                {field:'agentPoint',align:'center',width:200,title:'出单点/代理点'},
                {field:'companyCode',align:'center',width:200,title:'公司代码'},
                {field:'commercialCode',align:'center',width:150,title:'商户号'},
                {field:'state',align:'center',width:120,title:'状态',formatter:statusFormatter}
            ]
        ]
    });
    //高级查询
    $("a[data-query]").on("click",function(){
        //获取表单 高级查询条件, 封装到 qo中, 并且重新加载 datagrid
        var macsiteP = $("[name=macsiteP]").val();
        var organizationNameP = $("[name=organizationNameP]").val();
        var bankNameSnP = $("[name=bankNameSnP]").val();
        var companyCodeP = $("[name=companyCodeP]").val();
        var commercialCodeP = $("[name=commercialCodeP]").val();
        var stateP = $("[name=stateP]").val();
        posDatagrid.datagrid("load",{
            macsiteP:macsiteP,
            organizationNameP:organizationNameP,
            bankNameSnP:bankNameSnP,
            companyCodeP:companyCodeP,
            commercialCodeP:commercialCodeP,
            stateP:stateP
        });
    });
    //对话框
    posDialog.dialog({
        width:270,
        height:440,
        buttons:'#pos_dialog_bt',
        modal:true,
        closed:true
    });
    //对话框
    posDialog2.dialog({
        width:270,
        height:440,
        buttons:'#pos2_dialog_bt',
        modal:true,
        closed:true
    });
    //对按钮进行统一事件监听
    $("a[data-cmd]").on("click",function(){
        var cmd = $(this).data("cmd");
        if(cmd){
            cmdObj[cmd]();
        }
    });
    //方法统一管理起来
    var cmdObj = {
        check:function () {
            var rowData = posDatagrid.datagrid("getSelected");
            console.log(rowData);
            if(rowData){
                //1.清空表单数据
                posForm2.form("clear");
                //2.设置对话框的标题
                posDialog2.dialog("setTitle","新增");
                //3.打开对话框
                posDialog2.dialog("open");
                if(rowData.organization) {
                    rowData["organization.id"] = rowData.organization.id;
                }
                if(rowData.bankName) {
                    rowData["bankName.id"] = rowData.bankName.id;
                }
                //4.回显数据
                posForm2.form("load",rowData);//基于同名匹配规则
            }else{
                $.messager.alert("温馨提示","请选择一条需要修改的数据.","warning");
            }
        },
        add:function(){
            //1.清空表单数据
            posForm.form("clear");
            //2.设置对话框的标题
            posDialog.dialog("setTitle","新增");
            //3.打开对话框
            posDialog.dialog("open");
        },
        edit:function(){
            var rowData = posDatagrid.datagrid("getSelected");
            if(rowData){
                //1.清空表单数据
                posForm.form("clear");
                //2.设置对话框的标题
                posDialog.dialog("setTitle","新增");
                //3.打开对话框
                posDialog.dialog("open");
                if(rowData.organization) {
                    rowData["organization.id"] = rowData.organization.id;
                }
                if(rowData.bankName) {
                    rowData["bankName.id"] = rowData.bankName.id;
                }
                //4.回显数据
                posForm.form("load",rowData);//基于同名匹配规则
            }else{
                $.messager.alert("温馨提示","请选择一条需要修改的数据.","warning");
            }
        },
        del:function(){
            var rowData = posDatagrid.datagrid("getSelected");
            if(rowData){
                $.messager.confirm("温馨提示","您确定需要删除该数据吗?",function(yes){
                    if(yes){
                        $.get("/pos/delete?id="+rowData.id,function(data){
                            if(data.success){
                                posDatagrid.datagrid("reload");
                                $.messager.alert("温馨提示",data.msg,"info");
                            }else{
                                $.messager.alert("温馨提示",data.msg,"error");
                            }
                        },"json")
                    }
                });
            }else{
                $.messager.alert("温馨提示","请选择需要删除的记录.","warning");
            }
        },
        save:function(){
            var url;
            var idVal = $("[name='id']").val();
            if(idVal){
                url = "/pos/update";
            }else{
                url = "/pos/save";
            }
            posForm.form("submit",{
                url:url,
                success:function(data){
                    data = $.parseJSON(data);
                    if(data.success){
                        //提示消息,当点确定的时候,关闭对话框,刷新数据表格
                        $.messager.alert("温馨提示",data.msg,"info",function(){
                            posDialog.dialog("close");
                            posDatagrid.datagrid("reload");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                }
            });
        },
        cancel:function(){
            posDialog.dialog("close");
        },
        cancel2:function(){
            posDialog2.dialog("close");
        },
        reload:function(){
            //刷新数据表格
            posDatagrid.datagrid("reload");
        }
    }
});
function statusFormatter(value,record,index){
    if(value==1){
        return "<font color='green'>已开启</font>";
    }else if(value==2){
        return "<font color='red'>未开启</font>";
    }
}
function organizationFormatter(value,record,index){
    if(value){
        return value.organizationName;
    }
    return value;
}
function banknameFormatter(value,record,index){
    if(value){
        return value.sn;
    }
    return value;
}